package allod.som.initializer

import allod.som.Lattice.Neuron
import xml.{NodeSeq}
import allod.som.Metric

/**
 * Created by IntelliJ IDEA.
 * User: allod
 * Date: 7 січ 2010
 * Time: 21:38:46
 */

case class LatticeSerializer(node: NodeSeq) extends Initializer {
  private val columns = (node \\ "columns").text.toInt
  private val rows = (node \\ "rows").text.toInt
  private val dimension = (node \\ "inputDimension").text.toInt

  private val neurons: Array[Array[Neuron]] = {
    val neuronNodes = node \\ "neuron"
    val result = new Array[Array[Neuron]](columns, rows)
    neuronNodes foreach {
      neuron =>
        val column = (neuron \ "column").text.toInt
        val row = (neuron \ "row").text.toInt
        val weightNodes = neuron \\ "value"
        val weights = for{
          valueNode <- weightNodes
        } yield valueNode.text.toDouble
        result(column)(row) = weights.toList
    }
    result
  }

  def inputDimension(): Int = dimension

  def metric: Metric = Metric((node \ "metric").text)

  def createNeuron(column: Int, row: Int): Neuron = neurons(column)(row)

  private def initializerType = "existing lattice"

  def toXML() =
    <initializer>
      <type>
        {initializerType}
      </type>
      <settings>
        <dimension>
          {inputDimension}
        </dimension>
      </settings>
    </initializer>


}